﻿var ORDER_SCROLLBEGIN = 0; //全局变量
$(function () {
    var openId = $("#WxOpenId").val();
    UserInfoMgr.setOpenId(openId, function (openId) {
        if (openId == null || openId == "") {
            //获取用户信息失败
            venderDialog("获取用户信息失败，请关闭浏览器重试。");
        } else {
            UserInfoMgr.weixinLogin(loginCallback, logoutCallback);
        }
    });
});

function loginCallback() {
    $("article.main").show();
    setOrderListWindowHeight();
    setOrdersListScrollListener();
    OrderMgr.setOrderChangeNotifyCallback(orderChangeNotifyCallback);
    firstGetOrderList();
}

function logoutCallback() {
    window.location.href = spliceRegisterUrl('/orderMgr/Index', '/UserCenter/index');
}

function setOrdersListScrollListener() {
    var ordersWindow = $(".orderListWindow");
    var ordersDoc = $(".orderListDoc");
    isScroll(ordersWindow, ordersDoc, function () {
        if (ORDER_SCROLLBEGIN > 0) {
            var curType = OrderMgr.getOrderListCurIndex();
            OrderMgr.getOrderList(curType, ORDER_SCROLLBEGIN, Number(OrderMgr.queryOrderNum), getOrderListCallback);
        }
    });
}

function orderChangeNotifyCallback(orderInfo) {
    var curListType = OrderMgr.getOrderListCurIndex();
    if ((OrderMgr.allOrderListType == curListType)
        || (Number(curListType) == Number(orderInfo.order_type))) {
        var orderList = OrderMgr.getOrderListCurIndex();
        getOrderListCallback(orderList);
    }
}

function turnBack() {
    //OrderMgr.resetOrderChangeNotifyCallback();
    window.location.href = '/UserCenter/Index';
}

function firstGetOrderList() {
    var curType = OrderMgr.getOrderListCurIndex();
    $("#list_" + curType).addClass("MineNavFocus");
    OrderMgr.getOrderList(curType, ORDER_SCROLLBEGIN, Number(OrderMgr.queryOrderNum), getOrderListCallback);
}

function clickOrderList(type) {
    var lastType = OrderMgr.getOrderListCurIndex();
    if (lastType != type) {
        $("#list_" + lastType).removeClass("MineNavFocus");
        $("#list_" + type).addClass("MineNavFocus");
        $('.orderListWindow').animate({ scrollTop: 0 }, 600);
        clearOrderList();
        OrderMgr.setOrderListCurIndex(type);
        OrderMgr.getOrderList(type, ORDER_SCROLLBEGIN, Number(OrderMgr.queryOrderNum), getOrderListCallback);
    }
}

function getOrderListCallback(orderList) {
    if (orderList != null && (orderList.length > 0)) {
        showOrderList(orderList);
        setOrderListWindowHeight();
        $(".noOrder").hide();
        $("#OrderList").show();        
    } else {
        $(".orderListWindow").css("height", "0px");
        $(".noOrder").show();
        $("#OrderList").hide();
    }
}

function clearOrderList() {
    ORDER_SCROLLBEGIN = 0;
    $("div#OrderList a").each(function () {
        $(this).remove();
    });
}

function orderItemGoodsNum(orderItem) {
    var goods_num = 0;
    for (var i = 0; i < orderItem.goods_info.length; i++) {
        goods_num += orderItem.goods_info[i].goods_code.length;
    }
    return goods_num;
}

function orderitemTotalPrice(orderItem) {
    var totalPrice = 0;
    for (var i = 0; i < orderItem.goods_info.length; i++) {
        totalPrice += orderItem.goods_info[i].goods_code.length * orderItem.goods_info[i].goods_info.price;
    }
    return totalPrice.toFixed(2);
}

function showFailPayOrder(orderItem) {
    var goods_num = orderItemGoodsNum(orderItem);
    var totalPrice = orderitemTotalPrice(orderItem);
    var $a = $(
            '<a id=\"order_' + orderItem.orderid + '\" onclick=\"clickOrder(\'' + orderItem.orderid + '\')\">'
            + '<section id=\"item_' + orderItem.orderid + '\" class=\"MinePart MinePartOne\">'
            + '<div class=\"MinePartTop\">'
            + '<span>' + orderItem.time + '</span>'
            + '<span id=\"payStatus_' + orderItem.orderid + '\">待支付</span>'
            + '</div>'
            + '<div class=\"MinePartGoods\">'
            + '<img src=\"' + orderItem.goods_info[0].goods_info.image + '\">'
            + '<span>共' + goods_num + '件商品</span>'
            + '<span>￥' + totalPrice + '</span>'
            + '</div>'
            + '<div id=\"btn_' + orderItem.orderid + '\" class=\"MinePartBtn\">'
            + '<input type=\"button\" value=\"去支付\">'
            + '</div>'
            + '</section>'
            + '</a>');
    $("div#OrderList").append($a);
}

function showUnpickedOrder(orderItem) {
    var goods_num = orderItemGoodsNum(orderItem);
    var totalPrice = orderitemTotalPrice(orderItem);
    var $a = $(
            '<a id=\"order_' + orderItem.orderid + '\" onclick=\"clickOrder(\'' + orderItem.orderid + '\')\">'
            + '<section id=\"item_' + orderItem.orderid + '\" class=\"MinePart MinePartTwo\">'
            + '<div class=\"MinePartTop\">'
            + '<span>' + orderItem.time + '</span>'
            + '<span>已支付</span>'
            + '</div>'
            + '<div class=\"MinePartGoods\">'
            + '<img src=\"' + orderItem.goods_info[0].goods_info.image + '\">'
            + '<span>共' + goods_num + '件商品</span>'
            + '<span>￥' + totalPrice + '</span>'
            + '</div>'
            + '<div id=\"btn_' + orderItem.orderid + '\" class=\"MinePartBtn\">'
            + '<input type=\"button\" value=\"去取货\">'
            + '</div>'
            + '</section>'
            + '</a>');
    $("div#OrderList").append($a);
}

function showPickedOrder(orderItem) {
    var goods_num = orderItemGoodsNum(orderItem);
    var totalPrice = orderitemTotalPrice(orderItem);
    var $a = $(
        '<a id=\"order_' + orderItem.orderid + '\" onclick=\"clickOrder(\'' + orderItem.orderid + '\')\">'
        + '<section id=\"item_' + orderItem.orderid + '\" class=\"MinePart MinePartThree\">'
        + '<div class=\"MinePartTop\">'
        + '<span>' + orderItem.time + '</span>'
        + '<span id=\"payStatus_' + orderItem.orderid + '\">已收货</span>'
        + '</div>'
        + '<div class=\"MinePartGoods AlreadyPickUp\">'
        + '<img src=\"' + orderItem.goods_info[0].goods_info.image + '\">'
        + '<span>共' + goods_num + '件商品</span>'
        + '<span>￥' + totalPrice + '</span>'
        + '</div>'
        + '</section>'
        + '</a>');
    $("div#OrderList").append($a);
}

function showOrderList(orderList) {
    for (var i = ORDER_SCROLLBEGIN; i < orderList.length; i++) {
        var orderItem = orderList[i];
        if (orderItem.order_type == OrderMgr.failPayOrderType) {
            showFailPayOrder(orderItem);
        } else if (orderItem.order_type == OrderMgr.unpickedOrderType) {
            showUnpickedOrder(orderItem);
        } else if (orderItem.order_type == OrderMgr.pickedOrderType) {
            showPickedOrder(orderItem);
        }
    }
    ORDER_SCROLLBEGIN = orderList.length;
}

function clickOrder(orderId) {
    var curOrderList = OrderMgr.getCurOrderList();
    if (null != curOrderList) {
        for (index in curOrderList) {
            if (orderId == curOrderList[index].orderid) {
                OrderMgr.setCurOrderInfo(curOrderList[index]);
                break;
            }
        }
    }
    window.location.href = '/OrderInfo/Index/';
}

function setOrderListWindowHeight() {
    //当前浏览器窗口高度 - header+货机标题 48 + 44 = 92
    var currentWindowHeight = $(window).height() - 92; //由于 .vender 的高度变化了
    //当前容器确定高度
    $(".orderListWindow").css("height", currentWindowHeight + "px");
}